<?php
function Discount($ID_product)
{
//require_once('functs.inc');
define('ABSOLUTE_SERVER_DOCUMENT_ROOT',$_SERVER['DOCUMENT_ROOT'].'/admin/');

  if(isset($_COOKIE[COOKIE_SHOP_USER_NAME_SURNAME]))
    {
    	$UserNameCookie = $_COOKIE[COOKIE_SHOP_USER_ID_PASSWD];
    	DecodeCookie($UserNameCookie,$ID_user,$U_Passwd);
    }
    else
    	$ID_user=0;
    
	$sql_psel= "SELECT SC_ID FROM products WHERE ID=".$ID_product;
	$sql_array_Prodct = mysql_fetch_object(mysql_query($sql_psel));
//************************autoimaticke zlavy**********************************************	        
	$automatic_discount=1;
	
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/modules/discount_aut/discount_aut.inc') 
AND file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/modules/top_user/functions_top.inc')){
		
	$sql_psel= "SELECT Price_automatic FROM discount_automatic WHERE ID_user =".$ID_user;
	if(mysql_num_rows(mysql_query($sql_psel))!=0)					
	{ 
		$sql_array_Prodct_automatic = mysql_fetch_object(mysql_query($sql_psel));
		
		$sql_psel= "SELECT Top_price FROM top_user WHERE ID_top=".$ID_user;
		if(mysql_num_rows(mysql_query($sql_psel))!=0)					
		{
	    	$sql_array_Prodct_top = mysql_fetch_object(mysql_query($sql_psel));
	    	if(($sql_array_Prodct_automatic->Price_automatic) > ($sql_array_Prodct_top->Top_price))
	    		$automatic_discount=0;
	    }
	    else{
	    		$automatic_discount=0;}
	}
}	
//************************autoimaticke zlavy**********************************************	
//ci je tovar v akiiii///////////
$sql_psel= "SELECT discount_value FROM discount WHERE ID_user=0 AND ID_category=0";
if(mysql_num_rows(mysql_query($sql_psel))==0)
	$category=$sql_array_Prodct->SC_ID;
else{
	$sql="SELECT * FROM product_actions WHERE PA_ProductID=".$ID_product;
    $exist=mysql_query($sql);
    if(mysql_num_rows($exist)==1)
       $category=0;
    else
       $category=$sql_array_Prodct->SC_ID;
    }
////////////////////////////////	   			
		  	$sql_psel= "SELECT discount_value FROM discount WHERE ID_user=".$ID_user." AND ID_category=".$sql_array_Prodct->SC_ID;
    		if(mysql_num_rows(mysql_query($sql_psel))==0 || $ID_user==0)					
			{
    			$sql_psel= "SELECT discount_value FROM discount WHERE ID_user=0 AND ID_category=".$category;
    					if(mysql_num_rows(mysql_query($sql_psel))==0)
							return 1;
						else{
							$sql_array_Prodct = mysql_fetch_object(mysql_query($sql_psel));
							return (100-$sql_array_Prodct->discount_value)/100;	
							}													
				
			}
    		else																
			{
			  	$sql_array_Prodct_User = mysql_fetch_object(mysql_query($sql_psel));
			  	//priorita osobnej zlavy uzivatela
				//return (100-$sql_array_Prodct_User->discount_value)/100;
			 
			  	//priorita najvacsiej zlavy ,uprednostni vacsiu zlavu default uzivatela
			  	$sql_psel= "SELECT discount_value FROM discount WHERE ID_user=0 AND ID_category=".$category;
    					if(mysql_num_rows(mysql_query($sql_psel))==0){
    						if($automatic_discount) return (100-$sql_array_Prodct_User->discount_value)/100;
							else 					return 1;
							
						}else{
							$sql_array_Prodct = mysql_fetch_object(mysql_query($sql_psel));
							if($sql_array_Prodct->discount_value > $sql_array_Prodct_User->discount_value){
								return (100-$sql_array_Prodct->discount_value)/100;	
							}else{
								if($automatic_discount) return (100-$sql_array_Prodct_User->discount_value)/100;
								else 					return (100-$sql_array_Prodct->discount_value)/100;
							}
						}
			}
}
function DiscountPercent($category_ID)
{
  $sql_psel= "SELECT discount_value FROM discount WHERE ID_user=0 AND ID_category=".$category_ID;
    		if(mysql_num_rows(mysql_query($sql_psel))==0)					
			{
			  		return 0;
			}else
			{
					$sql_array_Prodct = mysql_fetch_object(mysql_query($sql_psel));
					return $sql_array_Prodct->discount_value;	
			}
}
function DiscountPercentUSER($category_ID,$User_ID)
{//print "uzivatel ID-".$User_ID."vvv";
  $sql_psel= "SELECT discount_value FROM discount WHERE ID_user=".$User_ID." AND ID_category=".$category_ID;
    		if(mysql_num_rows(mysql_query($sql_psel))==0)					
			{
			  		return 0;
			}else
			{
					$sql_array_Prodct = mysql_fetch_object(mysql_query($sql_psel));
					return $sql_array_Prodct->discount_value;	
			}
}
function EditDiscountSC($User_ID,$category_ID,$discount_value)
{
  $sql_psel= "SELECT discount_value FROM discount WHERE ID_user=".$User_ID." AND ID_category=".$category_ID;
    		if(mysql_num_rows(mysql_query($sql_psel))==0)					
			{
					if($discount_value!=0)	
					  		{
					  		$sql="INSERT INTO discount (ID_user,ID_category,discount_value) VALUES (".$User_ID.",".$category_ID.",".$discount_value.")";
		    				mysql_query($sql);
		    				}
			}else
			{
					  if($discount_value!=0)	
					  		{
							$sql="UPDATE discount SET discount_value=".$discount_value." WHERE ID_category=".$category_ID." AND ID_user=".$User_ID;
		    				mysql_query($sql);	
		    				}
		    				else
		    				{
		    				$sql="DELETE FROM discount WHERE ID_category=".$category_ID." AND ID_user=".$User_ID;
		    				mysql_query($sql);
		    				}	
			}
}
function EditDiscountTC($User_ID,$category_ID,$discount_value)
{
  $sql_submnu= "SELECT ID FROM sub_category WHERE TC_ID=".$category_ID;
  $SMnu_Source=mysql_query($sql_submnu);
   while($sql_array_SMnu = mysql_fetch_object($SMnu_Source))
	{
		  $sql_psel= "SELECT discount_value FROM discount WHERE ID_user=".$User_ID." AND ID_category=".$sql_array_SMnu->ID;
		    		if(mysql_num_rows(mysql_query($sql_psel))==0)					
					{
					  if($discount_value!=0)	
					  		{
					  		$sql="INSERT INTO discount (ID_user,ID_category,discount_value) VALUES (".$User_ID.",".$sql_array_SMnu->ID.",".$discount_value.")";
		    				mysql_query($sql);
		    				}
					}else
					{
					  if($discount_value!=0)	
					  		{
							$sql="UPDATE discount SET discount_value=".$discount_value." WHERE ID_category=".$sql_array_SMnu->ID." AND ID_user=".$User_ID;
		    				mysql_query($sql);	
		    				}
		    				else
		    				{
		    				$sql="DELETE FROM discount WHERE ID_category=".$sql_array_SMnu->ID." AND ID_user=".$User_ID;
		    				mysql_query($sql);
		    				}
					}
	}
}
function EditDiscountALL($User_ID,$discount_value)
{
  $sql_submnu= "SELECT ID FROM sub_category";
  $SMnu_Source=mysql_query($sql_submnu);
   while($sql_array_SMnu = mysql_fetch_object($SMnu_Source))
	{
		  $sql_psel= "SELECT discount_value FROM discount WHERE ID_user=".$User_ID." AND ID_category=".$sql_array_SMnu->ID;
		    		if(mysql_num_rows(mysql_query($sql_psel))==0)					
					{
					  if($discount_value!=0)	
					  		{
					  		$sql="INSERT INTO discount (ID_user,ID_category,discount_value) VALUES (".$User_ID.",".$sql_array_SMnu->ID.",".$discount_value.")";
		    				mysql_query($sql);
		    				}
					}else
					{
					  if($discount_value!=0)	
					  		{
							$sql="UPDATE discount SET discount_value=".$discount_value." WHERE ID_category=".$sql_array_SMnu->ID." AND ID_user=".$User_ID;
		    				mysql_query($sql);	
		    				}
		    				else
		    				{
		    				$sql="DELETE FROM discount WHERE ID_category=".$sql_array_SMnu->ID." AND ID_user=".$User_ID;
		    				mysql_query($sql);
		    				}
					}
	}
}
function EditDiscountPrice($User_ID,$discount_value)
{
$sql_psel= "SELECT Price_automatic FROM discount_automatic WHERE ID_user=".$User_ID;
    		if(mysql_num_rows(mysql_query($sql_psel))==0)					
			{
					if($discount_value!=0)	
					  		{
					  		$sql="INSERT INTO discount_automatic (ID_user,Price_automatic) VALUES (".$User_ID.",".$discount_value.")";
		    				mysql_query($sql);
		    				}
			}else
			{
					  if($discount_value!=0)	
					  		{
							$sql="UPDATE discount_automatic SET Price_automatic=".$discount_value." WHERE  ID_user=".$User_ID;
		    				mysql_query($sql);	
		    				}
		    				else
		    				{
		    				$sql="DELETE FROM discount_automatic WHERE ID_user=".$User_ID;
		    				mysql_query($sql);
		    				}	
			}
}
?>